
import { _decorator, Component, Node, find } from 'cc';
import { PlayerControllers } from './PlayerControllers';
import { MapMaker } from './MapMaker';
import { CameraFollow } from './CameraFollow';
import { UIMgr } from './UIMgr';

const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = GameMgr
 * DateTime = Sat Mar 05 2022 08:12:21 GMT+0800 (中国标准时间)
 * Author = lamd
 * FileBasename = GameMgr.ts
 * FileBasenameNoExtension = GameMgr
 * URL = db://assets/Scripts/GameMgr.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/en/
 *
 */

@ccclass('GameMgr')
export class GameMgr extends Component {
    // [2]
    // @property
    // serializableDummy = 0;
    public static instance: GameMgr = null!;
    map: MapMaker = null!;
    playerController: PlayerControllers = null!;
    player: Node = null!;
    camera: Node = null;
    isGameOver: boolean = false;
    uiMgr:UIMgr = null;

    onLoad() {
        if (GameMgr.instance == null)
            GameMgr.instance = this;
        else {
            console.log("warn 重复加载, node.name", this.node.name);
            this.destroy();
        }

        console.log("GameMgr start .................");

        this.map = find("Map").getComponent("MapMaker") as MapMaker;
        this.player = find("cube_books")
        this.playerController = this.player.getComponent("PlayerControllers") as PlayerControllers;

        this.camera = find("Main Camera");

        this.uiMgr = find("Canvas/StartUI").getComponent("UIMgr") as UIMgr;

        this.map.loadRes(() => {
            // this.playerController.init();
            // this.camera.getComponent(CameraFollow).init();
        });
    }

    start() {

    }

    gameStart() {
        this.gameReset()
        this.map.startDrop();
    }

    checkGameOverWithDrop() {
        let result: boolean = false;
        // 掉落
        if (this.map.dropRowIndex >= this.playerController.z)
            result = true

        // 踩到空洞位置， 游戏结束
        if (this.map.isHoldTile(this.playerController.x, this.playerController.z))
            result = true

        if (result) {
            this.playerController.dropPlayer()
            this.gameOver();
        }

        return result;
    }

    gameOver() {
        console.log("game over.");
        if (this.isGameOver) return;

        this.isGameOver = true; // 最后赋值
    }

    gameReset() {
        this.map.init();
        this.playerController.init();
        this.camera.getComponent(CameraFollow).init();
        
        this.isGameOver = false;
    }

    recycleOneRow() {
        this.map.getTileNode().splice(0, 1)
        this.map.dropRowIndex--;
        this.playerController.z--;
    }



    // update (deltaTime: number) {
    //     // [4]
    // }


}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/en/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/en/scripting/life-cycle-callbacks.html
 */
